package com.bwhk.splitdata;

import java.io.File;
import java.util.Properties;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;

@Configuration
public class mailss {
	public final String DEFALUT_ENCODING = "UTF-8";  

	@Value("${spring.email.host}")
	private  String springEmailHost;

	@Value("${spring.email.username}")
	private String springEmailUsername;

	@Value("${spring.email.password}")
	private String springEmailPassword;

	@Value("${spring.email.port}")
	private int springEmailPort;
	  
//    public static void main(String[] args) throws Exception {  
//        JavaMailSenderImpl sender = initJavaMailSender();  
//        String[] ss = { "ma.zhiguo.dky@jibei.sgcc.com.cn", "ma.zhiguo.dky@jibei.sgcc.com.cn" };  
//  
//        sendTextWithHtml(sender, ss, "测试简单文本邮件发送！ ", " 测试我的简单邮件发送机制！！ ");  
//  }  
  
    public void sendTextWithHtml( String[] tos, String subject, String text)  
            throws Exception {  
    	JavaMailSenderImpl sender = initJavaMailSender(); 
        MimeMessage mailMessage = sender.createMimeMessage();  
        initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text);  
        // 发送邮件  
        sender.send(mailMessage);  
  
        System.out.println("邮件发送成功..");  
    }  
  
//    public void sendTextWithImg(JavaMailSenderImpl sender, String[] tos, String subject, String text,  
//            String imgId, String imgPath) throws MessagingException {  
//        MimeMessage mailMessage = sender.createMimeMessage();  
//        MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text,  
//                true, true, "GBK");  
//        // 发送图片  
//        FileSystemResource img = new FileSystemResource(new File(imgPath));  
//        messageHelper.addInline(imgId, img);  
//        // 发送邮件  
//        sender.send(mailMessage);  
//  
//        System.out.println("邮件发送成功..");  
//    }  
//  
//    public  void sendWithAttament(JavaMailSenderImpl sender, String[] tos, String subject, String text,  
//            String AttachName, String filePath) throws MessagingException {  
//        MimeMessage mailMessage = sender.createMimeMessage();  
//        MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text,  
//                true, true, DEFALUT_ENCODING);  
//  
//        FileSystemResource file = new FileSystemResource(new File(filePath));  
//        // 发送邮件  
//        messageHelper.addAttachment(AttachName, file);  
//        sender.send(mailMessage);  
//  
//        System.out.println("邮件发送成功..");  
//  
//    }  
//  
//    public void sendWithAll(JavaMailSenderImpl sender, String[] tos, String from, String subject, String text,  
//            String imgId, String imgPath, String AttachName, String filePath) throws MessagingException {  
//        MimeMessage mailMessage = sender.createMimeMessage();  
//        MimeMessageHelper messageHelper = initMimeMessageHelper(mailMessage, tos, sender.getUsername(), subject, text,  
//                true, true, DEFALUT_ENCODING);  
//  
//        // 插入图片  
//        FileSystemResource img = new FileSystemResource(new File(imgPath));  
//        messageHelper.addInline(imgId, img);  
//        // 插入附件  
//        FileSystemResource file = new FileSystemResource(new File(filePath));  
//        messageHelper.addAttachment(AttachName, file);  
//  
//        // 发送邮件  
//        sender.send(mailMessage);  
//  
//        System.out.println("邮件发送成功..");  
//  
//    }  
  
    private MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,  
            String subject, String text) throws MessagingException {  
        return initMimeMessageHelper(mailMessage, tos, from, subject, text, true, false, DEFALUT_ENCODING);  
    }  
  
      
    private MimeMessageHelper initMimeMessageHelper(MimeMessage mailMessage, String[] tos, String from,  
            String subject, String text, boolean isHTML, boolean multipart, String encoding) throws MessagingException {  
        MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, multipart, encoding);  
        messageHelper.setTo(tos);  
        messageHelper.setFrom(from);  
        messageHelper.setSubject(subject);  
        // true 表示启动HTML格式的邮件  
        messageHelper.setText(text, isHTML);  
  
        return messageHelper;  
    }  
  
  
    /** 
     * 这个方法在实际应用中，springboot会通过在配置文件application.xml 
     * 中加配置自动实例化JavaMailSenderImpl，本方法只是为了测试使用 
     */  
    public JavaMailSenderImpl initJavaMailSender() {  
  
        Properties properties = new Properties();  
        properties.setProperty("mail.debug", "true");// 是否显示调试信息(可选)  
        properties.setProperty("mail.smtp.starttls.enable", "false");  
        properties.setProperty("mail.smtp.socketFactory.class",  
                "javax.net.ssl.SSLSocketFactory");  
        properties.setProperty("mail.smtp.auth", "true");  
        properties.put(" mail.smtp.timeout ", " 25000 ");  
  
        JavaMailSenderImpl javaMailSender = new JavaMailSenderImpl();  
  
        javaMailSender.setJavaMailProperties(properties);  
        javaMailSender.setHost(springEmailHost);  
        javaMailSender.setUsername(springEmailUsername); // s根据自己的情况,设置username  
        javaMailSender.setPassword(springEmailPassword); // 根据自己的情况, 设置password  
        javaMailSender.setPort(springEmailPort);  
        javaMailSender.setDefaultEncoding("UTF-8");  
  
        return javaMailSender;  
    }  
  
}
